package org.dromara.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.domain.MallCateBrand;
import org.dromara.mall.service.IMallCateBrandService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类品牌
 *
 * @author Lion Li
 * @date 2024-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/cateBrand")
public class MallCateBrandController extends BaseController {
    private final IMallCateBrandService mallCateBrandService;

    /**
     * 查询分类品牌列表
     */
    @SaCheckPermission("mall:cateBrand:list")
    @GetMapping("/list")
    public TableDataInfo<MallCateBrand> list(MallCateBrand bo, PageQuery pageQuery) {
        return mallCateBrandService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出分类品牌列表
     */
    @SaCheckPermission("mall:cateBrand:export")
    @Log(title = "分类品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallCateBrand bo, HttpServletResponse response) {
        List<MallCateBrand> list = mallCateBrandService.queryList(bo);
        ExcelUtil.exportExcel(list, "分类品牌", MallCateBrand.class, response);
    }

    /**
     * 获取分类品牌详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:cateBrand:query")
    @GetMapping("/{id}")
    public R<MallCateBrand> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mallCateBrandService.queryById(id));
    }

    /**
     * 新增分类品牌
     */
    @SaCheckPermission("mall:cateBrand:add")
    @Log(title = "分类品牌", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallCateBrand bo) {
        return toAjax(mallCateBrandService.insertByBo(bo));
    }

    /**
     * 修改分类品牌
     */
    @SaCheckPermission("mall:cateBrand:edit")
    @Log(title = "分类品牌", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallCateBrand bo) {
        return toAjax(mallCateBrandService.updateByBo(bo));
    }

    /**
     * 删除分类品牌
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:cateBrand:remove")
    @Log(title = "分类品牌", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallCateBrandService.deleteWithValidByIds(List.of(ids), true));
    }
}
